package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.ConfirmReceivingReq;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.ConfirmReceivingCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogPo;
import com.ymttest.database.sharding.db.sharding.model.PickInStorePo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SqConfirmReceiving {

	private static ConfirmReceivingReq sqConfirmReceivingReq;
	private static ConfirmReceivingCall sqConfirmReceivingCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("自提点批量确认收货");
	}

	@Before
	public void caseUp() {

		sqConfirmReceivingReq = new ConfirmReceivingReq();
		sqConfirmReceivingCall = new ConfirmReceivingCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqConfirmReceivingCall.getOpurl());
		
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verifyPickupStore(long orderId) {
		
		Logger.comment("验证pick_in_store表");
		
		PickInStorePo pickInStorePo = orderShardingWapper
				.selectPickInStorebyOrderId(orderId).get(0);

		Logger.verifyEquals((byte) 2, pickInStorePo.getPickupStatus(),
				"验证PickupStatus");
		
		Logger.comment("验证order_status_log表");
		OrderStatusLogPo orderStatusLogPo = orderShardingWapper.selectOrderStatusLogByOrderId(orderId);
		
		Logger.verifyEquals(1001, orderStatusLogPo.getOrderStatus(),
				"验证OrderStatus");
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqConfirmReceiving_001() {
		Logger.start(false, "商家通知自提，自提点确认收货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, 4321L, null);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			sqConfirmReceivingReq.setCsId(4321L);
			sqConfirmReceivingReq.setGoodsOwnerId(autotest_goodsOwnerId);
			sqConfirmReceivingReq.setOrderIds(Arrays.asList(orderId));
			
			sqConfirmReceivingCall.setData(sqConfirmReceivingReq);
			sqConfirmReceivingCall.callService();
			
			this.verifyPickupStore(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqConfirmReceiving_002() {
		Logger.start(false, "商家通知自提，未发货，自提点确认收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			sqConfirmReceivingReq.setCsId(4321L);
			sqConfirmReceivingReq.setGoodsOwnerId(autotest_goodsOwnerId);
			sqConfirmReceivingReq.setOrderIds(Arrays.asList(orderId));

			sqConfirmReceivingCall.setData(sqConfirmReceivingReq);
			sqConfirmReceivingCall.callService();

			Logger.verifyEquals(false, sqConfirmReceivingCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("订单状态有误，确认收货失败",
					sqConfirmReceivingCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqConfirmReceiving_003() {
		Logger.start(false, "商家通知自提，自提点多次确认收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, 4321L, null);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			sqConfirmReceivingReq.setCsId(4321L);
			sqConfirmReceivingReq.setGoodsOwnerId(autotest_goodsOwnerId);
			sqConfirmReceivingReq.setOrderIds(Arrays.asList(orderId));

			sqConfirmReceivingCall.setData(sqConfirmReceivingReq);
			sqConfirmReceivingCall.callService();
			
			sqConfirmReceivingCall.callService();

			Logger.verifyEquals(false, sqConfirmReceivingCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("订单状态有误，确认收货失败",
					sqConfirmReceivingCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqConfirmReceiving_004() {
		Logger.start(false, "买家确认自提，自提点确认收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 0);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			sqConfirmReceivingReq.setCsId(4321L);
			sqConfirmReceivingReq.setGoodsOwnerId(autotest_goodsOwnerId);
			sqConfirmReceivingReq.setOrderIds(Arrays.asList(orderId));

			sqConfirmReceivingCall.setData(sqConfirmReceivingReq);
			sqConfirmReceivingCall.callService();

			Logger.verifyEquals(false, sqConfirmReceivingCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("订单状态有误，确认收货失败",
					sqConfirmReceivingCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqConfirmReceiving_005() {
		Logger.start(false, "买家确认自提，已支付订单，自提点确认收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 0);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			Logger.comment("前置数据准备结束");

			sqConfirmReceivingReq.setCsId(4321L);
			sqConfirmReceivingReq.setGoodsOwnerId(autotest_goodsOwnerId);
			sqConfirmReceivingReq.setOrderIds(Arrays.asList(orderId));

			sqConfirmReceivingCall.setData(sqConfirmReceivingReq);
			sqConfirmReceivingCall.callService();

			Logger.verifyEquals(false, sqConfirmReceivingCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("订单状态有误，确认收货失败",
					sqConfirmReceivingCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqConfirmReceiving_006() {
		Logger.start(false, "商家通知自提，多个订单，自提点确认收货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			Logger.comment("第一个订单");
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, 4321L, null);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("第二个订单");
			TradingServiceResp placeOrderResp1 = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			long orderId2 = placeOrderResp1.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice2 = placeOrderResp1.getPayableAmount();

			// 支付订单
			Long thirdTradingId2 = SqTradingCallService.payOrder(userId,
					placeOrderResp1.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId2),
					totalPrice2.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId2);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId2);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId2);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId2, 4321L, null);
			TradingSyncService.getSyncResult(orderId2);

			Logger.comment("前置数据准备结束");
			
			ArrayList<Long> orders = new ArrayList<>();

			orders.add(orderId);
			orders.add(orderId2);

			sqConfirmReceivingReq.setCsId(4321L);
			sqConfirmReceivingReq.setGoodsOwnerId(autotest_goodsOwnerId);
			sqConfirmReceivingReq.setOrderIds(orders);

			sqConfirmReceivingCall.setData(sqConfirmReceivingReq);
			sqConfirmReceivingCall.callService();

			for (int i = 0; i < orders.size(); i++)
				this.verifyPickupStore(orders.get(i));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
